package com.xm.clj.underserver.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.xm.clj.underserver.bean.GetMessage;
import com.xm.clj.underserver.bean.StartInfo;
import com.xm.clj.underserver.service.GameService;
import com.xm.clj.underserver.utils.ConfigUtil;
import com.xm.clj.underserver.webSocket.OneToManyWebSocket;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Component
public class GameServiceImpl implements GameService {

    private static Random random = new Random();

    @Override
    public void startAGame(StartInfo startInfo) {
        // 从词库中选择
        if (startInfo==null){
            System.out.println("info is null");
        }
        String[] category = ConfigUtil.get("category").split(";");
        int len = category.length;
        if (len>0){
            int num = OneToManyWebSocket.rooms.get(startInfo.getRoomId()).size();
            OneToManyWebSocket.curPlayers.put(startInfo.getRoomId(),num);
            // 随机选择一个类别
            int curCategory = random.nextInt(len);
            System.out.println("all: "+ConfigUtil.get(category[curCategory]));
            String[] curCateLib = ConfigUtil.get(category[curCategory]).split(";");
            // 随机选择两个词
            List<String> curWords = new ArrayList<>();
            // 平民词
            int pIndex = random.nextInt(curCateLib.length);
            curWords.add(curCateLib[pIndex]);
            // 卧底词
            int uIndex;
            // TODO
            while((uIndex = random.nextInt(curCateLib.length))!=pIndex){ }
            curWords.add(curCateLib[uIndex]);
            OneToManyWebSocket.roomWords.put(startInfo.getRoomId(),curWords);
            // 分配卧底
            List<Session> curUsers = OneToManyWebSocket.rooms.get(startInfo.getRoomId());
            List<String> curUnder = assignUnder(curUsers);
            List<Session> curU = new ArrayList<>();
            for (String str : curUnder){
                curU.add(curUsers.get(Integer.parseInt(str)));
            }

            // 分配平民
            List<Session> curP = new ArrayList<>();
            for (Session session:curUsers){
                if (!curU.contains(session)){
                    curP.add(session);
                }
            }
            // 记录本轮游戏信息 房间id  卧底session
            OneToManyWebSocket.roomUnder.put(startInfo.getRoomId(),curUnder);
            // 发送信息给用户
            GetMessage getMessageP = new GetMessage();
            GetMessage getMessageU = new GetMessage();
            getMessageP.setId("1");
            getMessageP.setMes(curCateLib[pIndex]);
            getMessageU.setId("1");
            getMessageU.setMes(curCateLib[uIndex]);
            System.out.println("word: "+curCateLib[pIndex]);
            String str1 = JSON.toJSONString(getMessageP);
            String str2 = JSON.toJSONString(getMessageU);
//            byte[] byte1 = null;
//            byte[] byte2 = null;
//            try {
//                byte1 = str1.getBytes("UTF-8");
//                byte2 = str2.getBytes("UTF-8");
//                str1 = new String(byte1,"UTF-8");
//                str2 = new String(byte2,"UTF-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//            }

            OneToManyWebSocket.sendMessageToMany(str1,curP);
            OneToManyWebSocket.sendMessageToMany(str2,curU);
        }
    }

    @Override
    public void changeOwner(StartInfo startInfo) {

    }

    @Override
    public void exitRoom(StartInfo roomId) {

    }

    private static List<String> assignUnder(List<Session> curUsers){
        // 查房间内的玩家
        int userNum = curUsers.size();
        // 设置卧底数
        int uNum = userNum/5+1;
        List<String> under = new ArrayList<>();
        // 随机指定卧底
        int u1 = random.nextInt(userNum);
        under.add(""+u1);
        if (uNum>1){
            int u2;
            while((u2 = random.nextInt(userNum))!=u1){ }
            under.add(""+u2);
        }
        return under;
    }
}
